import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  GlobalKey<FormState> loginKey = GlobalKey();
  String username;
  String password;
  void login() {
    var loginForm = loginKey.currentState;

    if (loginForm.validate()) {
      loginForm.save();
      print("username: $username passwrod: $password");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("this is text"),
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.all(16.0),
            child: Form(
              key: loginKey,
              child: Column(
                children: [
                  TextFormField(
                    decoration: InputDecoration(
                      labelText: "请输入用户名",
                    ),
                    onSaved: (value) {
                      this.username = value;
                    },
                    onFieldSubmitted: (value) {},
                  ),
                  TextFormField(
                    decoration: InputDecoration(
                      labelText: "请输入密码",
                    ),
                    obscureText: true,
                    validator: (value) {
                      return value.length < 6 ? "密码长度不够" : null;
                    },
                    onSaved: (value) {
                      this.password = value;
                    },
                  ),
                ],
              ),
            ),
          ),
          SizedBox(
            width: 340.0,
            height: 42.0,
            child: ElevatedButton(
              child: Text("登录"),
              onPressed: login,
            ),
          )
        ],
      ),
    );
  }
}
